package com.lxmes.device.controller;

import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.core.domain.entity.SysUser;
import com.lxmes.common.core.page.TableDataInfo;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.device.domain.DeviceFailureRepair;
import com.lxmes.device.service.IDeviceFailureRepairService;
import com.lxmes.device.service.IDeviceFailureReportService;
import com.lxmes.device.utils.GetDate;
import com.lxmes.system.service.ISysDictDataService;
import com.lxmes.system.service.ISysPostService;
import com.lxmes.system.service.ISysUserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 设备故障维修单Controller
 *
 * @author 张志煜
 * @date 2022-04-20
 */
@Api(tags = "故障管理-故障维修")
@RestController
@RequestMapping("/device/deviceFailureRepair")
public class DeviceFailureRepairController extends BaseController
{
    @Autowired
    private IDeviceFailureRepairService deviceFailureRepairService;
    @Autowired
    private IDeviceFailureReportService deviceFailureReportService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDictDataService dataService;
    @Autowired
    private ISysPostService postService;
    /**
     * 查询设备故障维修单列表
     */
    @PreAuthorize("@ss.hasPermi('device:deviceFailureRepair:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeviceFailureRepair deviceFailureRepair)
    {
        startPage();
        List<DeviceFailureRepair> list = deviceFailureRepairService.selectDeviceFailureRepairList(deviceFailureRepair);
        return getDataTable(list);
    }

    /**
     * 导出设备故障维修单列表
     */
    @PreAuthorize("@ss.hasPermi('device:deviceFailureRepair:export')")
    @Log(title = "设备故障维修单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceFailureRepair deviceFailureRepair)
    {
        List<DeviceFailureRepair> list = deviceFailureRepairService.selectDeviceFailureRepairList(deviceFailureRepair);
        ExcelUtil<DeviceFailureRepair> util = new ExcelUtil<DeviceFailureRepair>(DeviceFailureRepair.class);
        util.exportExcel(response, list, "设备故障维修单数据");
    }

    /**
     * 获取设备故障维修单详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:deviceFailureRepair:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(deviceFailureRepairService.selectDeviceFailureRepairById(id));
    }

    /**
     * 新增设备故障维修单
     */
    @PreAuthorize("@ss.hasPermi('device:deviceFailureRepair:add')")
    @Log(title = "设备故障维修单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceFailureRepair deviceFailureRepair)
    {
        return toAjax(deviceFailureRepairService.insertDeviceFailureRepair(deviceFailureRepair));
    }

    /**
     * 修改设备故障维修单
     */
    @PreAuthorize("@ss.hasPermi('device:deviceFailureRepair:edit')")
    @Log(title = "设备故障维修单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceFailureRepair deviceFailureRepair)
    {
        return toAjax(deviceFailureRepairService.updateDeviceFailureRepair(deviceFailureRepair));
    }

    /**
     * 删除设备故障维修单
     */
    @PreAuthorize("@ss.hasPermi('device:deviceFailureRepair:remove')")
    @Log(title = "设备故障维修单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deviceFailureRepairService.deleteDeviceFailureRepairByIds(ids));
    }
    //撤单
    @PostMapping(value = "/cancelReport/{reportId}")
    @PreAuthorize("@ss.hasPermi('device:deviceFailureRepair:cancelReport')")
    public AjaxResult cancelReport(@PathVariable("reportId") String reportId)
    {
        return toAjax(deviceFailureRepairService.cancelReport(reportId));
    }
    /**
     * 变更工作人员
     */
    @PostMapping(value = "/updateJobSub/{reportId}/{majorId}/{fzMajorId}")
    @PreAuthorize("@ss.hasPermi('device:deviceFailureRepair:jobSub')")
    public AjaxResult addJobSub(@PathVariable("reportId") String reportId, @PathVariable("majorId") Long majorId, @PathVariable("fzMajorId") Long[] fzMajorId)
    {
        return toAjax(deviceFailureRepairService.updateDeviceFailureRepairJobSub(reportId,majorId,fzMajorId));
    }
    /*全部维修单*/
    @ApiOperation(value="全部故障维修单列表",notes = "状态代表1-待执行2-维修中3-待评价4-完成、不含待接单  时间:1-今天 2 本周 3 本月")
    //查询条件
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryParams", value = "维修单号、设备编码、设备名称、主修人", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "faultProgress", value = "状态", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "repairLevel", value = "维修级别", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "queryTime", value = "时间", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageSize", value = "分页大小",  required = true,paramType="query",  dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNum", value = "当前页数",  required = true,paramType="query",   dataType = "int", dataTypeClass = Integer.class)
    })

    @ApiResponses({@ApiResponse(code=200,message = "0k",response = DeviceFailureRepair.class)})
    @PreAuthorize("@ss.hasPermi('mobile:repair:list')")
    @GetMapping("/apiList")
    public TableDataInfo apiList(
            @RequestParam(value="queryParams",required = false) String queryParams,
            @RequestParam(value="queryTime",required = false) String queryTime,
            @RequestParam(value="faultProgress",required = false) String faultProgress,
            @RequestParam(value="repairLevel",required = false) String repairLevel
    )
    {
        DeviceFailureRepair deviceFailureRepair=new DeviceFailureRepair();
        if(StringUtils.isNotBlank(queryTime)){
            String startTime="";
            String endTime="";
            if(StringUtils.equals("1",queryTime)){
                startTime= DateUtils.getDate();
                endTime=DateUtils.getDate();
            }else if((StringUtils.equals("2",queryTime))){
                startTime= GetDate.getWeekFirstDay();
                endTime=GetDate.getWeekLastDay();

            }else if ((StringUtils.equals("3",queryTime))){
                startTime= GetDate.getMonthFirstDay();
                endTime=GetDate.getMonthLastDay();
            }
            deviceFailureRepair.setStartTime(startTime);
            deviceFailureRepair.setEndTime(endTime);
        }
        deviceFailureRepair.setQueryParams(queryParams);
        deviceFailureRepair.setRepairLevel(repairLevel);
        deviceFailureRepair.setFaultProgress(faultProgress);
        startPage();
        List<DeviceFailureRepair> list = deviceFailureRepairService.selectApiDeviceFailureRepairList(deviceFailureRepair);
        return getDataTable(list);
    }
    /*待派单列表*/
    @ApiOperation(value="待派单列表",notes = "状态代表0待派单 1-待执行2-维修中3-待验证4-完成5-驳货")
    //查询条件
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryParams", value = "设备编码、设备名称、主修人", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageSize", value = "分页大小",  required = true,paramType="query",  dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNum", value = "当前页数",  required = true,paramType="query",   dataType = "int", dataTypeClass = Integer.class)
    })

    @ApiResponses({@ApiResponse(code=200,message = "0k",response = DeviceFailureRepair.class)})
    @PreAuthorize("@ss.hasPermi('mobile:repair:list0')")
    @GetMapping("/apiProgress0List")
    public TableDataInfo apiProgress0List(
            @RequestParam(value="queryParams",required = false) String queryParams)
    {
        DeviceFailureRepair deviceFailureRepair=new DeviceFailureRepair();
        deviceFailureRepair.setQueryParams(queryParams);
        deviceFailureRepair.setFaultProgress("0");
        startPage();
        List<DeviceFailureRepair> list = deviceFailureRepairService.selectApiDeviceFailureRepairProgressList(deviceFailureRepair);
        return getDataTable(list);
    }
    /*待执行列表*/
    @ApiOperation(value="待执行列表",notes = "状态代表0待派单 1-待执行2-维修中3-待验证4-完成5-驳货")
    //查询条件
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryParams", value = "设备编码、设备名称、主修人", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageSize", value = "分页大小",  required = true,paramType="query",  dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNum", value = "当前页数",  required = true,paramType="query",   dataType = "int", dataTypeClass = Integer.class)
    })
    @ApiResponses({@ApiResponse(code=200,message = "0k",response = DeviceFailureRepair.class)})
    @PreAuthorize("@ss.hasPermi('mobile:repair:list1')")
    @GetMapping("/apiProgress1List")
    public TableDataInfo apiProgress1List(
            @RequestParam(value="queryParams",required = false) String queryParams)
    {
        DeviceFailureRepair deviceFailureRepair=new DeviceFailureRepair();
        deviceFailureRepair.setQueryParams(queryParams);
        deviceFailureRepair.setFaultProgress("1");
         boolean isAdmin= postService.selectPostListByUserIdIsAdmin(SecurityUtils.getUserId());
            if(!isAdmin){
                deviceFailureRepair.setUserId(SecurityUtils.getUserId());
            }
        startPage();
        List<DeviceFailureRepair> list = deviceFailureRepairService.selectApiDeviceFailureRepairProgressList(deviceFailureRepair);
        return getDataTable(list);
    }
    /*维修中/驳回列表*/
    @ApiOperation(value="维修中/驳回列表",notes = "状态代表0待派单 1-待执行2-维修中3-待验证4-完成5-驳货")
    //查询条件
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryParams", value = "设备编码、设备名称、主修人", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageSize", value = "分页大小",  required = true,paramType="query",  dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNum", value = "当前页数",  required = true,paramType="query",   dataType = "int", dataTypeClass = Integer.class)
    })
    @ApiResponses({@ApiResponse(code=200,message = "0k",response = DeviceFailureRepair.class)})
    @PreAuthorize("@ss.hasPermi('mobile:repair:list2')")
    @GetMapping("/apiProgress2List")
    public TableDataInfo apiProgress2List(
            @RequestParam(value="queryParams",required = false) String queryParams)
    {
        DeviceFailureRepair deviceFailureRepair=new DeviceFailureRepair();
        deviceFailureRepair.setQueryParams(queryParams);
        deviceFailureRepair.setFaultProgress("2");
        boolean isAdmin= postService.selectPostListByUserIdIsAdmin(SecurityUtils.getUserId());
        if(!isAdmin){
            deviceFailureRepair.setUserId(SecurityUtils.getUserId());
        }
        startPage();
        List<DeviceFailureRepair> list = deviceFailureRepairService.selectApiDeviceFailureRepairProgressList(deviceFailureRepair);
        return getDataTable(list);
    }
    /*待派单、待执行、维修中列表*//*
    @ApiOperation(value="待派单、待执行、维修中列表",notes = "状态代表0待派单 1-待执行2-维修中")
    //查询条件
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryParams", value = "设备编码、设备名称、主修人", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "faultProgress", value = "状态", required = true,paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageSize", value = "分页大小",  required = true,paramType="query",  dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNum", value = "当前页数",  required = true,paramType="query",   dataType = "int", dataTypeClass = Integer.class)
    })

    @ApiResponses({@ApiResponse(code=200,message = "0k",response = DeviceFailureRepair.class)})
    @GetMapping("/apiProgressList")
    public TableDataInfo apiProgressList(
            @RequestParam(value="queryParams",required = false) String queryParams,
            @RequestParam(value="faultProgress",required = true) String faultProgress
    )
    {
        DeviceFailureRepair deviceFailureRepair=new DeviceFailureRepair();
        deviceFailureRepair.setQueryParams(queryParams);
        deviceFailureRepair.setFaultProgress(faultProgress);
        startPage();
        List<DeviceFailureRepair> list = deviceFailureRepairService.selectApiDeviceFailureRepairProgressList(deviceFailureRepair);
        return getDataTable(list);
    }*/
    @ApiOperation(value="获取维修班组人员")
    @PreAuthorize("@ss.hasPermi('mobile:repair:queryUserPost')")
    @GetMapping("/getApiUserPostList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nickName", value = "用户名称", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageSize", value = "分页大小",  required = true,paramType="query",  dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNum", value = "当前页数",  required = true,paramType="query",   dataType = "int", dataTypeClass = Integer.class)
    })
    public TableDataInfo userPostList(@RequestParam(value="nickName",required = false) String nickName)
    {
        SysUser user=new SysUser();
        user.setNickName(nickName);
        startPage();
        List<SysUser> list = sysUserService.selectUserPostList(user);
        return getDataTable(list);
    }
  /*  @GetMapping("/getApiUserPost")
    public AjaxResult getApiUserPost(@RequestParam(value="nickName",required = false) String nickName){
        SysUser user=new SysUser();
        user.setNickName(nickName);
        return AjaxResult.success( sysUserService.selectUserPostList(user));
    }*/
    @ApiOperation(value="指派维修人员操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportId", value = "报修单号", required = true,paramType="path", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "majorId", value = "主修人id",  required = true,paramType="path",  dataType = "long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "fzMajorId", value = "辅修人id", paramType="path",   dataType = "long", dataTypeClass = Long.class)
    })
    @PreAuthorize("@ss.hasPermi('mobile:repair:jobSub')")
    @PostMapping(value = "/apiAddJobSub/{reportId}/{majorId}/{fzMajorId}")
    public AjaxResult apiAddJobSub(@PathVariable("reportId") String reportId, @PathVariable("majorId") Long majorId, @PathVariable("fzMajorId") Long[] fzMajorId) throws Exception {
        return toAjax(deviceFailureReportService.insertDeviceFailureReportJobSub(reportId,majorId,fzMajorId));
    }
    @ApiOperation(value="获取字典是否停机")
    @GetMapping("/getApiDictDataIsShutDown")
    public AjaxResult getDeviceIsFlag(){
        return AjaxResult.success( dataService.selectDictDataByType("device_is_shut_down"));
    }
    @ApiOperation(value="获取字典是否更换配件")
    @GetMapping("/getApiDictDataIsGhpj")
    public AjaxResult getApiDictDataIsGhpj(){
        return AjaxResult.success( dataService.selectDictDataByType("device_is_ghpj"));
    }
    @ApiOperation(value="获取维修班组")
    @GetMapping("/getApiWxDept")
    public AjaxResult getApiWxDept(){
        return AjaxResult.success( postService.selectPostById(5L));
    }
    @ApiOperation(value="获取字典维修状态")
    @GetMapping("/getApiDictDataRepairState")
    public AjaxResult getApiDictDataRepairState(){
        return AjaxResult.success( dataService.selectDictDataByType("device_repair_state"));
    }
    @ApiOperation(value="获取字典故障原因")
    @GetMapping("/getApiDictDataFailureCause")
    public AjaxResult getApiDictDataFailureCause(){
        return AjaxResult.success( dataService.selectDictDataByType("device_failure_cause"));
    }
    @ApiOperation(value="获取字典故障类别")
    @GetMapping("/getApiDictDataFaultType")
    public AjaxResult getApiDictDataFaultType(){
        return AjaxResult.success( dataService.selectDictDataByType("device_fault_type"));
    }
    @ApiOperation(value="获取字典维修级别")
    @GetMapping("/getApiDictDataRepairLevel")
    public AjaxResult getApiDictDataRepairLevel(){
        return AjaxResult.success( dataService.selectDictDataByType("device_repair_level"));
    }
    @ApiOperation(value="获取字典维修类别")
    @GetMapping("/getApiDictDataRepairType")
    public AjaxResult getApiDictDataRepairType(){
        return AjaxResult.success( dataService.selectDictDataByType("device_repair_type"));
    }

    @ApiOperation(value="维修故障确认")
    @Log(title = "设备故障维修单", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('mobile:repair:confirm')")
    @PostMapping(value = "/apiAddConfirm")
    public AjaxResult apiAddConfirm(@RequestBody DeviceFailureRepair deviceFailureRepair) throws IOException {
        return toAjax(deviceFailureRepairService.apiUpdateConfirm(deviceFailureRepair));
    }
    @ApiOperation(value="开始维修操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportId", value = "报修单号", required = true,paramType="path", dataType = "String", dataTypeClass = String.class)
    })
    @PreAuthorize("@ss.hasPermi('mobile:repair:start')")
    @PostMapping(value = "/apiStartRepair/{reportId}")
    public AjaxResult apiStartRepair(@PathVariable("reportId") String reportId)
    {
        return toAjax(deviceFailureRepairService.updateApiStartRepair(reportId));
    }
    /**
     * 新增设备故障报修单
     */
    @ApiOperation(value="添加故障维修操作")
    @Log(title = "设备故障维修单", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('mobile:repair:add')")
    @PostMapping("/apiAdd")
    public AjaxResult apiAdd(@RequestBody DeviceFailureRepair deviceFailureRepair) throws Exception {
        return toAjax(deviceFailureRepairService.updateApiDeviceFailureRepair(deviceFailureRepair));
    }
    /**
     * API获取设备故障报修单详细信息
     */
    @ApiOperation(value="故障维修单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportId", value = "报修单号", required = true,paramType="path", dataType = "String", dataTypeClass = String.class),
    })
    @PreAuthorize("@ss.hasPermi('mobile:repair:info')")
    @GetMapping(value = "/getRepairInfo/{reportId}")
    public AjaxResult getRepairInfo(@PathVariable("reportId") String reportId)
    {
        return AjaxResult.success(deviceFailureRepairService.selectDeviceFailureRepairByReportId(reportId));
    }
    /**
     * API撤单操作
     */
    @ApiOperation(value="故障维修撤单操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportId", value = "报修单号", required = true,paramType="path", dataType = "String", dataTypeClass = String.class),
    })
    @PreAuthorize("@ss.hasPermi('mobile:repair:delete')")
    @PostMapping(value = "/apiCancelReport/{reportId}")
    public AjaxResult apiCancelReport(@PathVariable("reportId") String reportId)
    {
        return toAjax(deviceFailureRepairService.cancelReport(reportId));
    }

}
